TtlCache
Keeps the answers to a remote question for as long as they are worth keeping.
构造函数
public TtlCache(TimeSpan ttl, TimeSpan maxAge, TimeProvider time)
ttlCache = TtlCache(ttl, maxAge, time)
Initializes a new instance of the TtlCache.
- ttl
- How long an answer is kept after it was last asked for.
- maxAge
- The oldest an answer may be, however often it is asked for.
- time
- The clock an answer's age is measured on.
public TtlCache(TimeSpan ttl, TimeSpan maxAge, TimeProvider time, IEqualityComparer<TKey> comparer)
ttlCache = TtlCache(ttl, maxAge, time, comparer)
Initializes a new instance of the TtlCache.
- ttl
- How long an answer is kept after it was last asked for.
- maxAge
- The oldest an answer may be, however often it is asked for.
- time
- The clock an answer's age is measured on.
- comparer
- How keys are compared.
属性
public int Count { get; }
value = ttlCache.Count
Gets the number of answers that have not gone stale.
public int HeldCount { get; }
value = ttlCache.HeldCount
Gets the number of answers held, stale ones included.
public IEnumerable<TValue> Values { get; }
value = ttlCache.Values
Gets the answers held that have not gone stale.
方法
public ValueTask<TValue> GetAsync(TKey key, Func<TKey, CancellationToken, ValueTask<TValue>> resolve, CancellationToken cancellationToken)
result = ttlCache.GetAsync(key, resolve, cancellationToken)
Serves the answer for a key, asking when there is none to serve.
- key
- What the answer is looked up by.
- resolve
- Where the answer comes from.
- cancellationToken
- Cancellation token.
返回值: The answer.
public ValueTask<TValue> GetAsync(TKey key, Func<TKey, CancellationToken, ValueTask<TValue>> resolve, TimeSpan ttl, TimeSpan maxAge, CancellationToken cancellationToken)
result = ttlCache.GetAsync(key, resolve, ttl, maxAge, cancellationToken)
Serves the answer for a key, asking when there is none to serve, and holds what it answered for a lifetime of this call's choosing.
- key
- What the answer is looked up by.
- resolve
- Where the answer comes from.
- ttl
- How long this answer is served before the source is asked again.
- cancellationToken
- Cancellation token.
返回值: The answer.
public bool Remove(TKey key)
result = ttlCache.Remove(key)
Drops the answer held for a key, for a caller that knows it has changed.
- key
- What the answer is looked up by.
返回值: true if an answer was held; otherwise, false.
public int RemoveWhere(Func<TKey, bool> match)
result = ttlCache.RemoveWhere(match)
Drops every answer whose key the caller names.
- match
- What the keys to drop have in common.
返回值: How many were dropped.
public int RemoveWhere(Func<TKey, TValue, bool> match)
result = ttlCache.RemoveWhere(match)
Drops every answer the caller recognises by what was cached.
- match
- What the answers to drop have in common.
返回值: How many were dropped.
public void Set(TKey key, TValue value)
ttlCache.Set(key, value)
Holds an answer for a key, for the lifetime this cache was made with.
- key
- What the answer is looked up by.
- value
- The answer.
public void Set(TKey key, TValue value, TimeSpan ttl, TimeSpan maxAge)
ttlCache.Set(key, value, ttl, maxAge)
Holds an answer for a key, for a lifetime of this call's choosing.
- key
- What the answer is looked up by.
- value
- The answer.
- ttl
- How long it is kept after it was last asked for.
- maxAge
- The oldest it may be, however often it is asked for.
public bool TryGet(TKey key, TValue value)
result = ttlCache.TryGet(key, value)
Serves the answer held for a key, for a caller that puts them there itself.
- key
- What the answer is looked up by.
- value
- The answer.
返回值: true if one is held and has not gone stale; otherwise, false.